$(function () {
  AllIncomingPayment = 1;
  getAllIncomingPayment()
})
const parlaikuanmingxiTableItemName = localStorage.getItem("laikuanmingxiTableItemName")
let AllFrom = localStorage.getItem("AllFrom")
console.log(AllFrom)
document.getElementById("Title-name").onclick = function () {
  window.location.href = "Statistics-2022.html"
}
//在当前选中 切换时间 
document.getElementById('year') ? layui.use(['laydate'], function (laydate) {
  laydate.render({
    elem: '#year',
    type: 'month',
    theme: '#165DFF',
    value: fullYear + "-" + fullMonth,
    //,range: true 
    //,trigger: 'click'
    done: function (value, date, endDate, tabIndex = 1) {
      fullYear = value.split('-')[0]
      fullMonth = value.split('-')[1]

      localStorage.setItem('year', fullYear);
      localStorage.setItem('month', fullMonth);
      change()
    },

  });
}) : ''

let tabIndex = 0;
let page = 1;
let limit = 10;
let tableHeight = 470
WLDW();

function getAllIncomingPayment() {
  //左侧(来款项目预算总额)
  var obj1 = {
    projectLb: parlaikuanmingxiTableItemName,
    budgetYear: fullYear,
    budgetMonth: fullMonth,
    dept: fullDepartment,
    status: 2,
    chartPage: 70605,
    chartNo: 5
  }
  ApiGet("getdata", obj1, function (e) {
    var json = JSON.parse(e);
    let data = json.items[0].charts[0].data;
    if (data.length > 0) {
      document.querySelector('.vessel .aside').innerHTML = `
    <div class="title">
      <span><a style="color: #86909C;" href="Statistics-Incoming.html">来款情况</a>/</span>
      <span><a style="color: #86909C;" href="budget-IncomingPayment.html">${AllFrom}</a>/</span>
      <h4> ${parlaikuanmingxiTableItemName} </h4>
    </div>
    <div class="budget">
      <h3>来款预算总额</h3>
      <h2>${data[0].key01}<span>万元</span></h2>
    </div>
    <div class="statistics">
      <h4>实际来款总额</h4>
      <h3>${data[0].key02}<span>万元</span></h3>
    </div>
    <div class="statistics">
      <h4>完成率</h4>
      <h3>${data[0].key03}%</h3>
    <div class="progress-bar">
      <div style="width: ${data[0].key03 >= 100 ? 100 : data[0].key03}%; background-color:${data[0].key03 >= 100 ? "#F53F3F" : "#165DFF"}" class="progress"></div>
      </div>
    </div>`
    } else {
      document.querySelector('.vessel .aside').innerHTML = ` <div class="title">
      <span><a style="color: #86909C;" href="Statistics-Incoming.html">来款情况</a>/</span>
      <span><a style="color: #86909C;" href="budget-IncomingPayment.html">${AllFrom}</a>/</span>
      <h4> ${parlaikuanmingxiTableItemName} </h4>
    </div>
    <div class="budget">
      <h3>来款预算总额</h3>
      <h2>--<span>万元</span></h2>
    </div>
    <div class="statistics">
      <h4>实际来款总额</h4>
      <h3>--<span>万元</span></h3>
    </div>
    <div class="statistics">
      <h4>完成率</h4>
      <h3>--</h3>`
    }
  });


  //tabs切换
  var tabs = document.querySelectorAll(".weidu-tab >div")
  for (var i = 0; i < tabs.length; i++) {
    tabs[i].setAttribute("index", i)
    tabs[i].onclick = function () {
      tabIndex = this.getAttribute("index")
      $(this).addClass('activeing').siblings().removeClass('activeing')
      console.log(tabIndex)
      if (tabIndex == 0) {
        document.getElementById("search").placeholder = '请输入单位名称'
        document.getElementById("search").value = ''
        WLDW()
      }
      if (tabIndex == 1) {
        document.getElementById("search").placeholder = '请输入部门名称'
        document.getElementById("search").value = ''
        BM()
      }
      if (tabIndex == 2) {
        document.getElementById("search").placeholder = '请输入合同名称'
        document.getElementById("search").value = ''
        HT()
      }
      if (tabIndex == 3) {
        document.getElementById("search").placeholder = '请输入任务号'
        document.getElementById("search").value = ''
        RWH()
      }
    }
  }
}

function change() {
  if (tabIndex == 0) {
    getAllIncomingPayment()
    WLDW()
  }
  if (tabIndex == 1) {
    getAllIncomingPayment()
    BM()
  }
  if (tabIndex == 2) {
    getAllIncomingPayment()
    HT()
  }
  if (tabIndex == 3) {
    getAllIncomingPayment()
    RWH()
  }
}

function WLDW() {
  var obj = {
    projectLb: parlaikuanmingxiTableItemName,
    budgetYear: fullYear,
    budgetMonth: fullMonth,
	  dept: fullDepartment,
    status: 2,
    chartPage: 70605,
    chartNo: 1,
    pageNum: page, //页号
    pageRowCount: limit, //每页多少条
  };
  ApiGet("getdata", obj, function (e) {
    var json = JSON.parse(e);
    let data = json.items[0].charts[0].data;
    WLDWList(data)
  });
}

function WLDWList(Tabledata, inputValue) {
  console.log(Tabledata, 'WLDW 列表')
  let data = Tabledata
  let cols = [
    [{
        field: 'key02',
        title: '单位名称',
        width: '22%',
        templet: function (d) {
          return `
        <a class="dwname" onclick="clickTo(this)" name=${d.key02}  "cursor:pointer">
          <div>
            <span>${d.key02}</span> 
          </div>
        </a>`
        }
      },
      {
        field: 'key03',
        title: '单位编号',
        width: '12%',
        sort: true,
      },
      {
        field: 'key04',
        title: '来款金额',
        sort: true,
        align: 'right',
        width: '12%',
      },
      {
        title: '来款统计',
        align: 'center',
        width: '8%',
        templet: function (d) {
          return `
        <a onclick="WLDWlaikuantongji(this)" name=${d.key02}  "cursor:pointer">
          <div class="icon" style="cursor: pointer;">
            <img src="assets/images/index/chat-poll-line.png" alt="">
          </div>
          <div class="hover-icon" style="cursor: pointer;">
            <img src="assets/images/index/chat-poll-line-hover.png" alt="">
          </div>
        </a>`
        }
      },
      {
        field: 'key05',
        title: '说明',
        width: '26%',
      },
      {
        title: '编辑说明',
        align: 'center',
        width: '8%',
        templet: function (d) {
          return `
        <a class="Tedit" onclick="WLDWExplain(this)" name=${d.key02} edit=${d.key05} >
          <div class="icon" style="cursor: pointer;">
          <img src="assets/images/index/edit.png" alt="">
          </div>
          <div class="hover-icon" style="cursor: pointer;">
          <img src="assets/images/index/edit-hover.png" alt="">
          </div>
        </a>`
        }
      }
    ],
  ];
  tableDrawing(data) // 数据传到 table组件
  pagesDrawing(data) // 数据传到 分页组件
  //表内容
  function tableDrawing(data) {
    layui.use('table', function () {
      let table = layui.table;
      table.render({
        elem: '#newsList',
        cols: cols,
        data: data, // 数据
        limit: limit, // 每页显示的条数
        height: tableHeight,
        // even: true, //开启隔行背景
        done: function (res, curr, count) {
          var that = this.elem.next();
          res.data.forEach(function (item, index) {
            if (index % 2 == 0) {
              var tr = that.find(".layui-table-box tbody tr[data-index= " + index + "]").css("background-color", "#FFF");
            } else {
              var tr = that.find(".layui-table-box tbody tr[data-index= " + index + "]").css("background-color", "#F3F7FF");
            }
          })
        }
      });
    })
  }
  //表脚
  function pagesDrawing(data) {
    let totolPage = data[0].key06 ? data[0].key06 : ''
    layui.use('laypage', function () {
      let laypage = layui.laypage;
      laypage.render({
        elem: 'laypage', //注意，这里的 page 是 ID，不用加 # 号
        count: totolPage, //数据总数，从服务端得到
        theme: '#E8F3FF',
        limit: limit, // 每页条数
        prev: '<em><</em>',
        next: '<em>></em>',
        layout: ['count', 'limit', 'prev', 'page', 'next', 'skip'],
        jump: function (obj, first) {
          console.log(obj, '第几页 ')
          console.log(obj.curr, '第几页 ')
          //首次不执行
          if (!first) {
            limit = obj.limit;
            numpage(obj.curr, obj.limit) // 分页点击传参 
          }
        }
      });
    })
  };
  //分页
  function numpage(curr, page_count) { // 分页切换-数据请求
    var obj = {
      projectLb: parlaikuanmingxiTableItemName,
      budgetYear: fullYear,
      budgetMonth: fullMonth,
	  dept: fullDepartment,
      status: 2,
      chartPage: 70605,
      chartNo: 1,
      chartNo: inputValue ? 6 : 1,
      pageNum: curr, //页号
      pageRowCount: page_count, //每页多少条
      wldw: inputValue ? inputValue : '',
    };
    ApiGet("getdata", obj, function (e) {
      var json = JSON.parse(e);
      var chartData = json.items[0].charts;
      let data = chartData[0].data
      console.log(data, '分页之后的数据')
      tableDrawing(data);
    });

  };
}

function WLDWSearch(inputValue) {
  var obj = {
    projectLb: parlaikuanmingxiTableItemName,
    budgetYear: fullYear,
    budgetMonth: fullMonth,
	dept: fullDepartment,
    wldw: inputValue,
    status: 2,
    chartPage: 70605,
    chartNo: 6,
    pageNum: page, //页号
    pageRowCount: limit, //每页多少条
  };
  ApiGet("getdata", obj, function (e) {
    var json = JSON.parse(e);
    var chartData = json.items[0].charts;
    let data = chartData[0].data
    WLDWList(data, inputValue);
    console.log(data, '搜索--WLDWList')
  });
}

function WLDWlaikuantongji(chartsName) {
  toggle()
  $(".chatrts-title h3").text(chartsName.getAttribute('name'))
  $(".chatrts-content h3").text('月度来款情况')
  var obj = {
    projectLb: parlaikuanmingxiTableItemName,
    budgetYear: fullYear,
    budgetMonth: fullMonth,
	dept: fullDepartment,
    status: 2,
    chartPage: 70601,
    chartNo: 2,
    wldw: chartsName.getAttribute('name'),
  }
  ApiGet("getdata", obj, function (e) {
    var json = JSON.parse(e);
    let WLDWlaikuantongjidata = json.items[0].charts[0].data;
    console.log(WLDWlaikuantongjidata, 'WLDW 月度累计来款')

    let xData = [];
    let currYearTotal = [];
    let currYearBudget = [];
    let increase = [];
    let legendData = ["来款金额", "已认领金额", "完成率"];
    if (WLDWlaikuantongjidata.length > 0) {
      for (var i = 0; i < WLDWlaikuantongjidata.length; i++) {
        xData.push(WLDWlaikuantongjidata[i].key01);
        currYearTotal.push(WLDWlaikuantongjidata[i].key02);
        currYearBudget.push(WLDWlaikuantongjidata[i].key03);
        increase.push(WLDWlaikuantongjidata[i].key04);
      }
    }
    let Data = [{
        name: "来款金额",
        type: "bar",
        barWidth: '30%',
        barGap: 0,
        itemStyle: {
          color: "#165DFF",
        },
        data: currYearTotal,
      },
      {
        name: "已认领金额",
        type: "bar",
        barWidth: '30%',
        barGap: 0,
        itemStyle: {
          color: "#50CD89",
        },
        data: currYearBudget,
      },
      {
        name: "完成率",
        type: "line",
        smooth: true,
        showSymbol: false, //是否默认展示圆点
        barWidth: "13",
        barGap: 0,
        yAxisIndex: 1,
        itemStyle: {
          color: "#7239EA",
        },
        data: increase,
      },
    ];
    have("mask-chatrts") ? IncomingPaymentCharts("mask-chatrts", xData, legendData, Data) : "";
  })
}

function WLDWExplain(chartsName) {
  document.querySelector('.mask-write-area textarea').value = chartsName.getAttribute('edit')
  toggleEdit()
  document.querySelector('.mask-content-edit .btn button.save').onclick = () => {
    //点击确认之后 
    var obj = {
      projectLb: parlaikuanmingxiTableItemName,
      budgetYear: fullYear,
      budgetMonth: fullMonth,
	  dept: fullDepartment,
      wldw: chartsName.getAttribute('name'),
      status: 2,
      chartPage: 70601,
      chartNo: 4,
      key01: document.querySelector(
        '.mask-write-area textarea').value,
    }
    ApiGet("getdata", obj, function (e) {
      toggleEdit()
      //渲染数据 
      if ($('.table-search input').val() !== '') {
        WLDWSearch($('.table-search input').val())
      } else {
        WLDW()
      }
    })
  }
}

function BM() {
  var obj = {
    projectLb: parlaikuanmingxiTableItemName,
    budgetYear: fullYear,
    budgetMonth: fullMonth,
	dept: fullDepartment,
    status: 2,
    chartPage: 70605,
    chartNo: 2,
    pageNum: page, //页号
    pageRowCount: limit, //每页多少条
  };
  ApiGet("getdata", obj, function (e) {
    var json = JSON.parse(e);
    let data = json.items[0].charts[0].data;
    BMList(data)
  });
}

function BMList(Tabledata, inputValue) {
  console.log(Tabledata, 'BM 列表')
  let data = Tabledata
  let cols = [
    [{
        field: 'key02',
        title: '部门名称',
        templet: function (d) {
          return `
        <a class="dwname" onclick="clickTo(this)" name=${d.key02}  "cursor:pointer">
          <div>
            <span>${d.key02}</span> 
          </div>
        </a>`
        }
      },
      {
        field: 'key03',
        title: '部门编号',
        sort: true,
      }, ,
      {
        field: 'key04',
        title: '负责人',
        templet: function (d) {
          return `
          <div style="width:47px">
            <span>${d.key04}</span> 
          </div>
        `
        }
      },
      {
        field: 'key05',
        title: '认领金额',
        sort: true,
        align: 'right',
        templet: function (d) {
          return `
          <div style="text-align: right;">
            <span style="color:#1D2129;font-size:14px">${d.key05}</span> 
          </div>
        `
        }
      },
      {
        title: '认领统计',
        align: 'center',
        templet: function (d) {
          return `
        <a onclick="BMlaikuantongji(this)" name=${d.key02} style="text-align: center;">
          <div class="icon" style="cursor: pointer;">
            <img src="assets/images/index/chat-poll-line.png" alt="">
          </div>
          <div class="hover-icon" style="cursor: pointer;">
            <img src="assets/images/index/chat-poll-line-hover.png" alt="">
          </div>
        </a>`
        }
      },
      {
        field: 'key05',
        title: '说明',
        templet: function (d) {
          return `
          <div style="text-align: left;">
            <span style="color:#4E5969;font-size:14px">${d.key06}</span> 
          </div>
        `
        }
      },
      {
        title: '编辑说明',
        align: 'center',
        templet: function (d) {
          return `
        <a style="text-align: center;" onclick="BMExplain(this)" name=${d.key02} edit=${d.key06}  >
          <div class="icon" style="cursor: pointer;">
          <img src="assets/images/index/edit.png" alt="">
          </div>
          <div class="hover-icon" style="cursor: pointer;">
          <img src="assets/images/index/edit-hover.png" alt="">
          </div>
        </a>`
        }
      }
    ],
  ];
  tableDrawing(data) // 数据传到 table组件
  pagesDrawing(data) // 数据传到 分页组件
  //表内容
  function tableDrawing(data) {
    layui.use('table', function () {
      let table = layui.table;
      table.render({
        elem: '#newsList',
        cols: cols,
        data: data, // 数据
        limit: limit, // 每页显示的条数
        height: tableHeight,
        // even: true, //开启隔行背景
        done: function (res, curr, count) {
          var that = this.elem.next();
          res.data.forEach(function (item, index) {
            if (index % 2 == 0) {
              var tr = that.find(".layui-table-box tbody tr[data-index= " + index + "]").css("background-color", "#FFF");
            } else {
              var tr = that.find(".layui-table-box tbody tr[data-index= " + index + "]").css("background-color", "#F3F7FF");
            }
          })
        }
      });
    })
  }
  //表脚
  function pagesDrawing(data) {
    let totolPage = data[0].key07 ? data[0].key07 : ''
    layui.use('laypage', function () {
      let laypage = layui.laypage;
      laypage.render({
        elem: 'laypage', //注意，这里的 page 是 ID，不用加 # 号
        count: totolPage, //数据总数，从服务端得到
        theme: '#E8F3FF',
        limit: limit, // 每页条数
        prev: '<em><</em>',
        next: '<em>></em>',
        layout: ['count', 'limit', 'prev', 'page', 'next', 'skip'],
        jump: function (obj, first) {
          console.log(obj, '第几页 ')
          console.log(obj.curr, '第几页 ')
          //首次不执行
          if (!first) {
            limit = obj.limit;
            numpage(obj.curr, obj.limit) // 分页点击传参 
          }
        }
      });
    })
  };
  //分页
  function numpage(curr, page_count) { // 分页切换-数据请求
    var obj = {
      projectLb: parlaikuanmingxiTableItemName,
      budgetYear: fullYear,
      budgetMonth: fullMonth,
      status: 2,
      chartPage: 70605,
      chartNo: 1,
      chartNo: inputValue ? 6 : 2,
      pageNum: curr, //页号
      pageRowCount: page_count, //每页多少条
      dept: inputValue ? inputValue : '',
    };
    ApiGet("getdata", obj, function (e) {
      var json = JSON.parse(e);
      var chartData = json.items[0].charts;
      let data = chartData[0].data
      console.log(data, '分页之后的数据')
      tableDrawing(data);
    });

  };
}

function BMSearch(inputValue) {
  var obj = {
    projectLb: parlaikuanmingxiTableItemName,
    budgetYear: fullYear,
    budgetMonth: fullMonth,
    dept: inputValue,
    status: 2,
    chartPage: 70605,
    chartNo: 7,
    pageNum: page, //页号
    pageRowCount: limit, //每页多少条
  };
  ApiGet("getdata", obj, function (e) {
    var json = JSON.parse(e);
    var chartData = json.items[0].charts;
    let data = chartData[0].data
    BMList(data, inputValue);
    console.log(data, '搜索--BMList')
  });
}

function BMlaikuantongji(chartsName) {
  toggle()
  $(".chatrts-title h3").text(chartsName.getAttribute('name'))
  $(".chatrts-content h3").text('月度认领情况')
  var obj = {
    projectLb: parlaikuanmingxiTableItemName,
    budgetYear: fullYear,
    budgetMonth: fullMonth,
    status: 2,
    chartPage: 70602,
    chartNo: 2,
    dept: chartsName.getAttribute('name'),
  }
  ApiGet("getdata", obj, function (e) {
    var json = JSON.parse(e);
    let BMlaikuantongjidata = json.items[0].charts[0].data;
    console.log(BMlaikuantongjidata, 'BM 认领统计')

    let xData = [];
    let currYearTotal = [];
    let currYearBudget = [];
    let increase = [];
    let legendData = ["来款金额", "本次认领金额", "完成率"];
    if (BMlaikuantongjidata.length > 0) {
      for (var i = 0; i < BMlaikuantongjidata.length; i++) {
        xData.push(BMlaikuantongjidata[i].key01);
        currYearTotal.push(BMlaikuantongjidata[i].key02);
        currYearBudget.push(BMlaikuantongjidata[i].key03);
        increase.push(BMlaikuantongjidata[i].key04);
      }
    }
    let Data = [{
        name: "来款金额",
        type: "bar",
        barWidth: '30%',
        barGap: 0,
        itemStyle: {
          color: "#165DFF",
        },
        data: currYearTotal,
      },
      {
        name: "本次认领金额",
        type: "bar",
        barWidth: '30%',
        barGap: 0,
        itemStyle: {
          color: "#50CD89",
        },
        data: currYearBudget,
      },
      {
        name: "完成率",
        type: "line",
        smooth: true,
        showSymbol: false, //是否默认展示圆点
        barWidth: "13",
        barGap: 0,
        yAxisIndex: 1,
        itemStyle: {
          color: "#7239EA",
        },
        data: increase,
      },
    ];
    have("mask-chatrts") ? IncomingPaymentCharts("mask-chatrts", xData, legendData, Data) : "";
  })
}

function BMExplain(chartsName) {
  document.querySelector('.mask-write-area textarea').value = chartsName.getAttribute('edit')
  toggleEdit()
  document.querySelector('.mask-content-edit .btn button.save').onclick = () => {
    //点击确认之后 
    var obj = {
      projectLb: parlaikuanmingxiTableItemName,
      budgetYear: fullYear,
      budgetMonth: fullMonth,
      dept: chartsName.getAttribute('name'),
      status: 2,
      chartPage: 70602,
      chartNo: 4,
      key01: document.querySelector(
        '.mask-write-area textarea').value,
    }
    ApiGet("getdata", obj, function (e) {
      toggleEdit()
      //渲染数据 
      if ($('.table-search input').val() !== '') {
        BMSearch($('.table-search input').val())
      } else {
        BM()
      }
    })
  }
}

function HT() {
  var obj = {
    projectLb: parlaikuanmingxiTableItemName,
    budgetYear: fullYear,
    budgetMonth: fullMonth,
	dept: fullDepartment,
    status: 2,
    chartPage: 70605,
    chartNo: 3,
    pageNum: page, //页号
    pageRowCount: limit, //每页多少条
  };
  ApiGet("getdata", obj, function (e) {
    var json = JSON.parse(e);
    let data = json.items[0].charts[0].data;
    HTList(data)
  });
}

function HTList(Tabledata, inputValue) {
  let data = Tabledata
  let cols = [
    [{
        field: 'key03',
        title: '合同名称',
        width: '22%',
        templet: function (d) {
          return `
        <a class="dwname" onclick="clickTo(this)" name=${d.key03}  "cursor:pointer">
          <div>
            <span>${d.key03}</span> 
          </div>
        </a>`
        }
      },
      {
        field: 'key02',
        title: '合同编号',
        width: '12%',
        sort: true,
      },
      {
        field: 'key04',
        title: '合同金额',
        sort: true,
        align: 'right',
        width: '12%',
      },
      {
        title: '认领统计',
        align: 'center',
        width: '8%',
        templet: function (d) {
          return `
        <a onclick="HTlaikuantongji(this)" name=${d.key03} >
          <div class="icon" style="cursor: pointer;">
            <img src="assets/images/index/chat-poll-line.png" alt="">
          </div>
          <div class="hover-icon" style="cursor: pointer;">
            <img src="assets/images/index/chat-poll-line-hover.png" alt="">
          </div>
        </a>`
        }
      },
      {
        field: 'key05',
        title: '说明',
        width: '26%',

      },
      {
        title: '编辑说明',
        align: 'center',
        width: '8%',
        templet: function (d) {
          return `
        <a  class="Tedit" onclick="HTExplain(this)" name=${d.key03} edit=${d.key05} >
          <div class="icon" style="cursor: pointer;">
          <img src="assets/images/index/edit.png" alt="">
          </div>
          <div class="hover-icon" style="cursor: pointer;">
          <img src="assets/images/index/edit-hover.png" alt="">
          </div>
        </a>`
        }
      }
    ],
  ];
  tableDrawing(data) // 数据传到 table组件
  pagesDrawing(data) // 数据传到 分页组件
  function tableDrawing(data) {
    layui.use('table', function () {
      let table = layui.table;
      table.render({
        elem: '#newsList',
        cols: cols,
        data: data, // 数据
        limit: limit, // 每页显示的条数
        height: tableHeight,
        // even: true, //开启隔行背景
        done: function (res, curr, count) {
          var that = this.elem.next();
          res.data.forEach(function (item, index) {
            if (index % 2 == 0) {
              var tr = that.find(".layui-table-box tbody tr[data-index= " + index + "]").css("background-color", "#FFF");
            } else {
              var tr = that.find(".layui-table-box tbody tr[data-index= " + index + "]").css("background-color", "#F3F7FF");
            }
          })
        }
      });
    })
  }

  function pagesDrawing(data) {
    let totolPage = data[0].key06 ? data[0].key06 : ''
    layui.use('laypage', function () {
      let laypage = layui.laypage;
      laypage.render({
        elem: 'laypage', //注意，这里的 page 是 ID，不用加 # 号
        count: totolPage, //数据总数，从服务端得到
        theme: '#E8F3FF',
        limit: limit, // 每页条数
        prev: '<em><</em>',
        next: '<em>></em>',
        layout: ['count', 'limit', 'prev', 'page', 'next', 'skip'],
        jump: function (obj, first) {
          console.log(obj, '第几页 ')
          console.log(obj.curr, '第几页 ')
          //首次不执行
          if (!first) {
            limit = obj.limit;
            numpage(obj.curr, obj.limit) // 分页点击传参 
          }
        }
      });
    })
  };

  function numpage(curr, page_count) { // 分页切换-数据请求
    var obj = {
      projectLb: parlaikuanmingxiTableItemName,
      budgetYear: fullYear,
      budgetMonth: fullMonth,
	  dept: fullDepartment,
      htbh: inputValue ? inputValue : '',
      status: 2,
      chartPage: 70605,
      chartNo: inputValue ? 8 : 3,
      pageNum: curr, //页号
      pageRowCount: page_count, //每页多少条
    };
    ApiGet("getdata", obj, function (e) {
      var json = JSON.parse(e);
      var chartData = json.items[0].charts;
      let data = chartData[0].data
      console.log(data, '分页之后的数据')
      tableDrawing(data);
    });

  };
}

function HTSearch(inputValue) {
  var obj = {
    projectLb: parlaikuanmingxiTableItemName,
    budgetYear: fullYear,
    budgetMonth: fullMonth,
	dept: fullDepartment,
    htbh: inputValue,
    status: 2,
    chartPage: 70605,
    chartNo: 8,
    pageNum: page, //页号
    pageRowCount: limit, //每页多少条
  };
  ApiGet("getdata", obj, function (e) {
    var json = JSON.parse(e);
    var chartData = json.items[0].charts;
    let data = chartData[0].data
    HTList(data, inputValue);
    console.log(data, '搜索--HTList')
  });
}

function HTlaikuantongji(chartsName) {
  toggle()
  $(".chatrts-title h3").text(chartsName.getAttribute('name'))
  $(".chatrts-content h3").text('月度认领情况')
  var obj = {
    projectLb: parlaikuanmingxiTableItemName,
    budgetYear: fullYear,
    budgetMonth: fullMonth,
	dept: fullDepartment,
    htbh: chartsName.getAttribute('name'),
    status: 2,
    chartPage: 70603,
    chartNo: 2
  }
  ApiGet("getdata", obj, function (e) {
    var json = JSON.parse(e);
    let HTlaikuantongjidata = json.items[0].charts[0].data;
    console.log(HTlaikuantongjidata, 'HT 认领统计')

    let xData = [];
    let currYearTotal = [];
    let currYearBudget = [];
    let increase = [];
    let legendData = ["合同金额", "合同已认领金额", "完成率"];
    if (HTlaikuantongjidata.length > 0) {
      for (var i = 0; i < HTlaikuantongjidata.length; i++) {
        xData.push(HTlaikuantongjidata[i].key01);
        currYearTotal.push(HTlaikuantongjidata[i].key02);
        currYearBudget.push(HTlaikuantongjidata[i].key03);
        increase.push(HTlaikuantongjidata[i].key04);
      }
    }
    let Data = [{
        name: "合同金额",
        type: "bar",
        barWidth: '30%',
        barGap: 0,
        itemStyle: {
          color: "#165DFF",
        },
        data: currYearTotal,
      },
      {
        name: "合同已认领金额",
        type: "bar",
        barWidth: '30%',
        barGap: 0,
        itemStyle: {
          color: "#50CD89",
        },
        data: currYearBudget,
      },
      {
        name: "完成率",
        type: "line",
        smooth: true,
        showSymbol: false, //是否默认展示圆点
        barWidth: "13",
        barGap: 0,
        yAxisIndex: 1,
        itemStyle: {
          color: "#7239EA",
        },
        data: increase,
      },
    ];
    have("mask-chatrts") ? IncomingPaymentCharts("mask-chatrts", xData, legendData, Data) : "";
  })
}

function HTExplain(chartsName) {
  document.querySelector('.mask-write-area textarea').value = chartsName.getAttribute('edit')
  toggleEdit()
  document.querySelector('.mask-content-edit .btn button.save').onclick = () => {
    //点击确认之后 
    var obj = {
      projectLb: parlaikuanmingxiTableItemName,
      budgetYear: fullYear,
      budgetMonth: fullMonth,
	  dept: fullDepartment,
      htbh: chartsName.getAttribute('name'),
      status: 2,
      chartPage: 70603,
      chartNo: 4,
      key01: document.querySelector(
        '.mask-write-area textarea').value,
    }
    ApiGet("getdata", obj, function (e) {
      toggleEdit()
      //渲染数据 
      if ($('.table-search input').val() !== '') {
        HTSearch($('.table-search input').val())
      } else {
        HT()
      }
    })
  }
}

function RWH() {
  var obj = {
    projectLb: parlaikuanmingxiTableItemName,
    budgetYear: fullYear,
    budgetMonth: fullMonth,
	dept: fullDepartment,
    status: 2,
    chartPage: 70605,
    chartNo: 4,
    pageNum: page, //页号
    pageRowCount: limit, //每页多少条
  };
  ApiGet("getdata", obj, function (e) {
    var json = JSON.parse(e);
    let data = json.items[0].charts[0].data;
    RWHList(data)
  });
}

function RWHList(Tabledata, inputValue) {
  let data = Tabledata
  let cols = [
    [{
        field: 'key03',
        title: '任务号',
        width: '22%',
        templet: function (d) {
          return `
        <a class="dwname" onclick="clickTo(this)" name=${d.key02}  "cursor:pointer">
          <div>
            <span>${d.key02}</span> 
          </div>
        </a>`
        }
      },
      {
        field: 'key02',
        title: '任务号名称',
        width: '12%',
        sort: true,
      },
      {
        field: 'key04',
        title: '任务号金额',
        sort: true,
        align: 'right',
        width: '12%',
      },
      {
        title: '认领统计',
        align: 'center',
        width: '8%',
        templet: function (d) {
          return `
        <a onclick="RWHlaikuantongji(this)" name=${d.key02}>
          <div class="icon" style="cursor: pointer;">
            <img src="assets/images/index/chat-poll-line.png" alt="">
          </div>
          <div class="hover-icon" style="cursor: pointer;">
            <img src="assets/images/index/chat-poll-line-hover.png" alt="">
          </div>
        </a>`
        }
      },
      {
        field: 'key05',
        title: '说明',
        width: '26%',

      },
      {
        title: '编辑说明',
        align: 'center',
        width: '8%',
        templet: function (d) {
          return `
        <a class="Tedit" onclick="RWHExplain(this)" name=${d.key02} edit=${d.key05}>
          <div class="icon" style="cursor: pointer;">
          <img src="assets/images/index/edit.png" alt="">
          </div>
          <div class="hover-icon" style="cursor: pointer;">
          <img src="assets/images/index/edit-hover.png" alt="">
          </div>
        </a>`
        }
      }
    ],
  ];
  tableDrawing(data) // 数据传到 table组件
  pagesDrawing(data) // 数据传到 分页组件
  function tableDrawing(data) {
    layui.use('table', function () {
      let table = layui.table;
      table.render({
        elem: '#newsList',
        cols: cols,
        data: data, // 数据
        limit: limit, // 每页显示的条数
        height: tableHeight,
        // even: true, //开启隔行背景
        done: function (res, curr, count) {
          var that = this.elem.next();
          res.data.forEach(function (item, index) {
            if (index % 2 == 0) {
              var tr = that.find(".layui-table-box tbody tr[data-index= " + index + "]").css("background-color", "#FFF");
            } else {
              var tr = that.find(".layui-table-box tbody tr[data-index= " + index + "]").css("background-color", "#F3F7FF");
            }
          })
        }
      });
    })
  }

  function pagesDrawing(data) {
    let totolPage = data[0].key06 ? data[0].key06 : ''
    layui.use('laypage', function () {
      let laypage = layui.laypage;
      laypage.render({
        elem: 'laypage', //注意，这里的 page 是 ID，不用加 # 号
        count: totolPage, //数据总数，从服务端得到
        theme: '#E8F3FF',
        limit: limit, // 每页条数
        prev: '<em><</em>',
        next: '<em>></em>',
        layout: ['count', 'limit', 'prev', 'page', 'next', 'skip'],
        jump: function (obj, first) {
          console.log(obj, '第几页 ')
          console.log(obj.curr, '第几页 ')
          //首次不执行
          if (!first) {
            limit = obj.limit;
            numpage(obj.curr, obj.limit) // 分页点击传参 
          }
        }
      });
    })
  };

  function numpage(curr, page_count) { // 分页切换-数据请求
    var obj = {
      projectLb: parlaikuanmingxiTableItemName,
      budgetYear: fullYear,
      budgetMonth: fullMonth,
	  dept: fullDepartment,
      status: 2,
      chartPage: 70605,
      chartNo: inputValue ? 9 : 4,
      pageNum: curr, //页号
      pageRowCount: page_count, //每页多少条
      rwh: inputValue ? inputValue : '',
    };
    ApiGet("getdata", obj, function (e) {
      var json = JSON.parse(e);
      var chartData = json.items[0].charts;
      let data = chartData[0].data
      console.log(data, '分页之后的数据')
      tableDrawing(data);
    });

  };
}

function RWHSearch(inputValue) {
  var obj = {
    projectLb: parlaikuanmingxiTableItemName,
    budgetYear: fullYear,
    budgetMonth: fullMonth,
	dept: fullDepartment,
    rwh: inputValue,
    status: 2,
    chartPage: 70605,
    chartNo: 9,
    pageNum: page, //页号
    pageRowCount: limit, //每页多少条
  };
  ApiGet("getdata", obj, function (e) {
    var json = JSON.parse(e);
    var chartData = json.items[0].charts;
    let data = chartData[0].data
    RWHList(data, inputValue);
    console.log(data, '搜索--RWHList')
  });
}

function RWHlaikuantongji(chartsName) {
  toggle()
  $(".chatrts-title h3").text(chartsName.getAttribute('name'))
  $(".chatrts-content h3").text('月度认领情况')
  var obj = {
    projectLb: parlaikuanmingxiTableItemName,
    budgetYear: fullYear,
    budgetMonth: fullMonth,
	dept: fullDepartment,
    rwh: chartsName.getAttribute('name'),
    status: 2,
    chartPage: 70604,
    chartNo: 2
  }
  ApiGet("getdata", obj, function (e) {
    var json = JSON.parse(e);
    let RWHlaikuantongjidata = json.items[0].charts[0].data;
    console.log(RWHlaikuantongjidata, 'RWH 认领统计')

    let xData = [];
    let currYearTotal = [];
    let currYearBudget = [];
    let increase = [];
    let legendData = ["到款金额", "任务号金额", "完成率"];
    if (RWHlaikuantongjidata.length > 0) {
      for (var i = 0; i < RWHlaikuantongjidata.length; i++) {
        xData.push(RWHlaikuantongjidata[i].key01);
        currYearTotal.push(RWHlaikuantongjidata[i].key02);
        currYearBudget.push(RWHlaikuantongjidata[i].key03);
        increase.push(RWHlaikuantongjidata[i].key04);
      }
    }
    let Data = [{
        name: "到款金额",
        type: "bar",
        barWidth: '30%',
        barGap: 0,
        itemStyle: {
          color: "#165DFF",
        },
        data: currYearTotal,
      },
      {
        name: "任务号金额",
        type: "bar",
        barWidth: '30%',
        barGap: 0,
        itemStyle: {
          color: "#50CD89",
        },
        data: currYearBudget,
      },
      {
        name: "完成率",
        type: "line",
        smooth: true,
        showSymbol: false, //是否默认展示圆点
        barWidth: "13",
        barGap: 0,
        yAxisIndex: 1,
        itemStyle: {
          color: "#7239EA",
        },
        data: increase,
      },
    ];
    have("mask-chatrts") ? IncomingPaymentCharts("mask-chatrts", xData, legendData, Data) : "";
  })
}

function RWHExplain(chartsName) {
  document.querySelector('.mask-write-area textarea').value = chartsName.getAttribute('edit')
  toggleEdit()
  document.querySelector('.mask-content-edit .btn button.save').onclick = () => {
    //点击确认之后 
    var obj = {
      projectLb: parlaikuanmingxiTableItemName,
      budgetYear: fullYear,
      budgetMonth: fullMonth,
	  dept: fullDepartment,
      rwh: chartsName.getAttribute('name'),
      status: 2,
      chartPage: 70604,
      chartNo: 4,
      key01: document.querySelector(
        '.mask-write-area textarea').value,
    }
    ApiGet("getdata", obj, function (e) {
      toggleEdit()
      //渲染数据 
      if ($('.table-search input').val() !== '') {
        RWHSearch($('.table-search input').val())
      } else {
        RWH()
      }
    })
  }
}


//搜索
function search() {
  let inputValue = document.getElementById("search").value
  console.log(inputValue, tabIndex, '搜索/tabindex')
  if (tabIndex == 0) {
    WLDWSearch(inputValue)
  }
  if (tabIndex == 1) {
    BMSearch(inputValue)
  }
  if (tabIndex == 2) {
    HTSearch(inputValue)
  }
  if (tabIndex == 3) {
    RWHSearch(inputValue)
  }
}
//回车搜索
$("#search").keyup(function (event) {
  if (event.keyCode == 13) {
    search()
  }
});
//给div加边框
function inputClick() {
  $("#search").parent("div").css({
    border: "1px solid #165DFF",
    backgroundColor: "#FFF"
  })
}

function toggle() {
  document.querySelector('.All-IncomingPayment-Mask').style.display = document.querySelector(
    '.All-IncomingPayment-Mask').style.display !== 'block' ? 'block' : 'none'
}

function toggleEdit() {
  // document.querySelector('.mask-write-area textarea').value = ''
  document.querySelector('.mask-position-edit').style.display = document.querySelector(
    '.mask-position-edit').style.display !== 'block' ? 'block' : 'none'
}
//编辑说明--取消
document.querySelector('.mask-content-edit .btn button.cancle').onclick = function () {
  toggleEdit()
}

//MaskClose 
function MaskClose() {
  console.log('mask关闭')
  toggle()
}

function clickTo(obj) {
  // console.log(obj.getAttribute("name"))
  localStorage.setItem("rootName", obj.getAttribute("name"))
  let fromGuanxitu = Number(tabIndex) + 1
  localStorage.setItem("fromGuanxitu", fromGuanxitu)
  window.location.href = "budgent-IncomingPayment-guanxitu.html"
}